//
//  FriendsView.m
//  DriftDog
//
//  Created by rimi on 15/9/10.
//  Copyright (c) 2015年 廖马林. All rights reserved.
//

#import "FriendsView.h"

static NSString *const KUITableViewCellIdentifier = @"KUITableViewCellIdentifier";

@interface FriendsView () <UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating> {

    NSMutableArray *_dataSource;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UISearchController *search;

@end

@implementation FriendsView

- (instancetype)init {
    
    self = [super initWithFrame:DHFlexibleFrame(CGRectMake(0, NAV_HEIGHT + 60, ORIGIN_WIDTH, ORIGIN_HEIGHT), YES)];
    CGRect frame = self.frame;
    frame.size.height = frame.size.height;
    self.frame = frame;
    if (self) {
        [self initializeAppearence];
    }
    return self;
}
#pragma mark 
#pragma mark - 界面
- (void)initializeAppearence {

    [self addSubview:self.tableView];
    self.tableView.tableHeaderView = self.search.searchBar;
}

#pragma mark
#pragma mark - <UITableViewDelegate, UITableViewDataSource> 
//配置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

//    return _dataSource.count;
    return 3;
}
//配置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}
//配置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KUITableViewCellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KUITableViewCellIdentifier];
    }
    //配置单元格显示内容
    
    return cell;
}

#pragma mark - <UISearchResultsUpdating>
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {

    [_dataSource removeAllObjects];
    if (!searchController.active) {
        [_dataSource addObjectsFromArray:@[]];
        
        return;
    }
    
    NSString *string = searchController.searchBar.text;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS [cd] %@", string];
    
    NSArray *tempArray = @[];
    
    NSArray *results = [NSArray arrayWithArray:[tempArray filteredArrayUsingPredicate:predicate]];
    
    [_dataSource addObjectsFromArray:results];
    
    [self.tableView reloadData];
}


#pragma mark
#pragma mark - getter
- (UITableView *)tableView {

    if (!_tableView) {
        _tableView = ({
        
            UITableView *tableView = [[UITableView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, ORIGIN_WIDTH, ORIGIN_HEIGHT), YES) style:UITableViewStyleGrouped];
            tableView.dataSource = self;
            tableView.delegate = self;
            tableView.bounces = NO;
            
            tableView;
        });
    }
    return _tableView;
}
- (UISearchController *)search {

    if (!_search) {
        _search = ({
        
            UISearchController *search = [[UISearchController alloc] initWithSearchResultsController:nil];
            search.searchBar.frame = DHFlexibleFrame(CGRectMake(0, 0, ORIGIN_WIDTH, 60), YES);
            search.searchResultsUpdater = self;
            
            //test
            search;
        });
    }
    return _search;
}
@end
